package eurekaclient.eurekaclient;

import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.logging.Level;
import java.util.logging.Logger;

@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaProviderApplication {

	private static final Logger LOG = Logger.getLogger(EurekaProviderApplication.class.getName());

	@Autowired
	private RestTemplate restTemplate;

	@Bean
	public RestTemplate getRestTemplate(){
		return new RestTemplate();
	}

	@Bean
	public Sampler defaultSampler() {
		return Sampler.ALWAYS_SAMPLE;
	}

	@Value("${spring.application.name}")
	String applicationName;

	@Value("${server.port}")
	String port;

	@RequestMapping("/hi")
	@ResponseBody
	public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
		LOG.log(Level.INFO, "calling trace eureka-provider ");
		//return restTemplate.getForObject("http://localhost:8081/info?name="+name,String.class);
		return  name+" hello world";
	}

	@RequestMapping("/info")
	public String info(){
		LOG.log(Level.INFO, "calling trace service-hi ");
		return "i'm service-hi";
	}

	@RequestMapping("/")
	public String home() {
		return "Hello world";
	}

	@RequestMapping(value = "/search/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Person searchPerson(@PathVariable Integer id){
		Person person = new Person();
		person.setId(id);
		person.setName("Spirit");
		return person;
	}

	/*public static void main(String[] args) {
		SpringApplication.run(EurekaClientApplication.class, args);
	}*/

	public static void main(String[] args) {
		new SpringApplicationBuilder(EurekaProviderApplication.class).web(true).run(args);
	}


}

